﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL_NhaSach;
using BLL;

namespace BLL_NhaSach
{
	public class PhieuNhapBLL : IProcessData
	{
		QLNhaSachEntities db;

		public PhieuNhapBLL(string cnStr)
		{
			db = new QLNhaSachEntities(cnStr);
		}

		public object getAllData()
		{
			return (from pn in db.PhieuNhaps select new { pn.MaPhieuNhap, pn.NgayNhap, pn.MaNV, pn.MaNCC });
		}

        public object findObjects(string find_content)
        {
            throw new NotImplementedException();
        }
        //public object findObjects(int find_content)
        //{
        //    return db.PhieuNhaps.SingleOrDefault(p => p.MaPhieuNhap == find_content);
        //}
        public object findObject(int find_content)
        {
            return db.PhieuNhaps.SingleOrDefault(p => p.MaPhieuNhap == find_content);
        }
        public int getNextID()
        {
            PhieuNhap pn = db.PhieuNhaps.OrderByDescending(p => p.MaPhieuNhap).FirstOrDefault();
            if (pn == null)
                return 1;
            else
            {
                int id = pn.MaPhieuNhap+1;
                return id;
            }
        }
		public object findObjects(int find_content)
		{
			return (db.PhieuNhaps.SingleOrDefault(p => p.MaPhieuNhap == find_content));
		}

		public object sort(string Col_Name, bool state)
		{
			throw new NotImplementedException();
		}

		public bool addObject(object addObj)
		{
			PhieuNhap pn = (PhieuNhap)addObj;
			if (pn != null)
			{
				try
				{
					db.PhieuNhaps.AddObject(pn);
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}

		public bool deleteObject(int id)
		{
			PhieuNhap pn = db.PhieuNhaps.SingleOrDefault(p => p.MaPhieuNhap == id);
			if (pn != null)
			{
				try
				{
					db.PhieuNhaps.DeleteObject(pn);
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}

		public bool updateObject(object updateObj)
		{
			PhieuNhap pn_new = (PhieuNhap)updateObj;
			PhieuNhap pn_old = db.PhieuNhaps.SingleOrDefault(p => p.MaPhieuNhap == pn_new.MaPhieuNhap);
			if (pn_old != null)
			{
				try
				{
					pn_old.NgayNhap = pn_new.NgayNhap;
					pn_old.NhaCungCap = pn_new.NhaCungCap;
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}
        public int getSoLuong(String id)
        {
            return db.Saches.SingleOrDefault(p => p.MaSach.Equals(id)).SoLuongTon;
        }
        public Boolean updateSoluong(String id, decimal giabia,int soluong){
            Sach s = db.Saches.SingleOrDefault(p => p.MaSach.Equals(id));
            if (s != null)
            {
                if(giabia!=0)
                    s.GiaBia = giabia;
                s.SoLuongTon = s.SoLuongTon + soluong;
                try
                {
                    db.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else return false;
        }
        public String getTen(String id)
        {
            return db.Saches.SingleOrDefault(p => p.MaSach.Equals(id)).TenSach;
        }
	}
}
